package models

import (
	"time"

	"gorm.io/gorm"
)

type Thread struct {
	ID        int `gorm:"primaryKey`
	Uuid      string
	Title     string
	UserID    int
	User      User
	Posts     []Post
	CreatedAt time.Time
}

func (t *Thread) BeforeCreate(tx *gorm.DB) error {
	t.Uuid = createUUID()
	return nil
}

func (t *Thread) NumReplies() (count int64) {
	Db.Model(&Post{}).Where("thread_id = ?", t.ID).Count(&count)
	return count
}

//Jan 2 15:04:05 2006 MST
func (t *Thread) CreatedDate() string {
	return t.CreatedAt.Format("2006-01-02 15:04:05")
}
